package com.nerve.secondhandcar.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ko.core.web.BaseController;
import org.ko.core.web.Result;
import org.ko.core.websupport.log.Log;
import org.ko.core.websupport.log.impl.LogUitls;
import org.ko.core.websupport.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nerve.secondhandcar.domain.Brand;
import com.nerve.secondhandcar.service.BrandService;

/**
 * 平台中的汽车品牌Controller
 * @author 集成显卡
 * @version 2014-10-09 17:26:50
 */
@Controller
@RequestMapping("/car/brand")
public class BrandController extends BaseController{

	@Autowired
	private BrandService service;
	
	@Override
	protected String getTemplatePath() {
		return "car/brand/";
	}

	@RequestMapping(value="add", method = RequestMethod.GET)
	public String add(ModelMap model,Long pid){
		//如果是添加子菜单,则将父菜单信息查询出来
		if(null != pid){
			model.addAttribute("parent",service.get(pid));
		}
		
		return view("add");
	}
	
	@RequiresPermissions("brand:add")
	@Log(message="增加品牌[name={0}]",params={"name"}, category=Log.BRAND)
	@RequestMapping(value="add", method = RequestMethod.POST)
	public String addDo(HttpServletRequest request, HttpServletResponse response, Brand bean){
		try {
			service.addDo(bean);
			return "redirect:list.do";
		} catch (Exception e) {
			e.printStackTrace();
			LogUitls.error(e);
			
			return redirect("add.do");
		}
	}
	
	@RequestMapping(value="edit",method = RequestMethod.GET)
	public String edit(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "id", required = true)Long id){
		
		model.addAttribute("entity",service.get(id));
		return view("edit");
	}
	
	/**
	 * 提交修改内容,完成后重定向到列表
	 * @param request
	 * @param response
	 * @param model
	 * @param bean
	 * @return
	 */
	@RequiresPermissions("brand:edit")
	@RequestMapping(value="edit",method = RequestMethod.POST)
	public String editDo(HttpServletRequest request,HttpServletResponse response,ModelMap model,Brand bean){
		
		service.editDo(bean);
		return "redirect:list.do";
	}
	
	/**
	 * 批量删除指定ID记录
	 * @param request
	 * @param response
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("brand:delete")
	@Log(message="删除品牌,id={0}",params={"id"},category=Log.BRAND)
	@RequestMapping(value="delete",method = RequestMethod.POST)
	public @ResponseBody Result delete(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "ids", required = true)Long ids[]){
		Result result = new Result();
		try {
			service.delete(ids);
			result.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(e.getMessage());
			
			LogUitls.error(e);
		}
		
		return result;
	}
	
	@RequestMapping(value="list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, ModelMap model, Page page,String searchText){
		return view(LIST);
	}
	
	
	/**
	 * 显示树列表
	 * @param request
	 * @param response
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("brand:tree")
	@RequestMapping(value="tree",method = RequestMethod.POST)
	public @ResponseBody Result tree(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,String searchText){
		
		
		Result result = new Result();
		try {
			result.setMessage("操作成功");
			List p = service.findTree(searchText);
			result.setRoot(p);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		
		return result;
	}
	/**
	 * 更换当前菜单的父菜单
	 * @param request
	 * @param response
	 * @param model
	 * @param ids
	 * @return
	 */
	@Log(message="更换品牌的上级品牌", category=Log.BRAND)
	@RequestMapping(value="changeParent",method = RequestMethod.POST)
	public @ResponseBody Result changeParent(
			HttpServletRequest request,
			HttpServletResponse response,Long id,Long pid){
		
		Result result = new Result();
		try {
			result.setSuccess(service.changeParent(id,pid));
			
			LogUitls.putLog("品牌id="+id+",上级品牌id="+pid);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMessage(e.getMessage());
			
			LogUitls.error(e);
		}
		return result;
	}
}